# -*- coding:utf-8 -*-
# --author： jingfeng 
# time: 2019/3/18



"""爬取京东商品"""


from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys  #
from selenium.webdriver import ActionChains
from selenium.webdriver.support import expected_conditions as EC  # 条件
from selenium.webdriver.support.wait import WebDriverWait
from lxml import etree
import time
browser = webdriver.Chrome()
wait = WebDriverWait(browser,20)

browser.get('https://www.jd.com/')

input = wait.until(EC.presence_of_element_located(
        (By.XPATH, '//div[@class="form"]/input'))
        )

input.send_keys('ipad')
input.send_keys(Keys.ENTER)

time.sleep(2)



mytree = etree.HTML(browser.page_source)

price = mytree.xpath(
        '//li/div[@class="gl-i-wrap"]/div[@class="p-price"]/strong/i/text()')
print(price)
# 评论


comment_num = mytree.xpath(
    '//li/div[@class="gl-i-wrap"]/div[@class="p-commit"]/strong/a/text()')

print(comment_num)
picture = mytree.xpath(
        '//li/div[@class="gl-i-wrap"]/div[@class="p-img"]/a/img/@src')
print(picture)
title = mytree.xpath(
    '//li/div[@class="gl-i-wrap"]/div[contains(@class,"p-name")]/a/em/text()')


print(title)
shop = mytree.xpath(
        '//li/div[@class="gl-i-wrap"]/div[@class="p-shop"]/span/a/text()')
print(shop)


# 翻页

input_page = wait.until(
        EC.presence_of_element_located(
                (By.XPATH,"*[@id='J_bottomPage']/span[@class='p-skip']/input")))
input_page.clear()
input_page.send_keys()
input_page.send_keys(Keys.ENTER)